package com.icare.common.service.autoconfig;

import com.icare.common.service.db.IdWorker;
import com.icare.common.service.properties.IdWorkProperties;
import com.icare.common.service.utils.IdWorkerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * IdWork自动装配类
 *
 * @author TianHaibo
 * @create 2018-01-12 17:46
 **/
@Slf4j
@Configuration
@ConditionalOnProperty(
        prefix = "idworker",
        name = {"workId"}
)
@EnableConfigurationProperties({IdWorkProperties.class})
public class IdWorkAutoConfig {

    @Autowired
    private IdWorkProperties idWorkProperties;

    @Bean
    IdWorker idWorker(){
        IdWorker idWorker = new IdWorker(idWorkProperties.getWorkId());
        log.info("init idworker ,id = {}",idWorkProperties.getWorkId());
        IdWorkerUtil.setIdWorker(idWorker);
        return idWorker;
    }
}
